package com.yida.auth.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.yida.auth.form.LoginBody;
import com.yida.auth.service.BasicDataService;
import com.yida.auth.service.SysSmsService;
import com.yida.common.core.domain.R;
import com.yida.common.core.utils.bean.BeanUtils;
import com.yida.system.api.domain.SysSms;

/**
 * 
 * 授权中心基础数据
 * 
 * @author hmh
 * @date 2022/10/26
 */
@RestController
public class AuthBasicDataController {

    @Autowired
    private BasicDataService basicDataService;

    @Autowired
    private SysSmsService sysSmsService;

    @PostMapping("basicConfig")
    public R<?> basicConfig() {
        Map<String, Object> basicDataMap = basicDataService.basicConfig();
        return R.ok(basicDataMap);
    }

    @PostMapping("sendSmsCode")
    public R<?> sendSmsCode(@RequestBody LoginBody form) {
        Map<String, Object> sysSmsResultMap = sysSmsService.sendSmsCode(form.getUsername(), form.getPhonenumber());
        if (!"ok".equals(sysSmsResultMap.get("sms"))) {
            return R.ok(sysSmsResultMap);
        }
        return R.ok("验证码发送成功！");
    }

    @PostMapping("sendSms")
    public R<?> sendSms(@RequestBody SysSms sysSms) {
        // Map<String, Object> sysSmsResultMap = sysSmsService.sendSmsCode(form.getUsername(), form.getPhonenumber());
        // if (!"ok".equals(sysSmsResultMap.get("sms"))) {
        // return R.ok(sysSmsResultMap);
        // }
        // System.out.println("sysSms:" + JSON.toJSONString(sysSms));
        SysSms rSysSms = new SysSms();
        BeanUtils.copyBeanProp(rSysSms, sysSms);
        Map<String, Object> sysSmsResultMap = sysSmsService.sendSms(rSysSms, sysSms.getSmsSource());
        if (!"ok".equals(sysSmsResultMap.get("sms"))) {
            return R.ok(sysSmsResultMap);
        }
        return R.ok("验证码发送成功！");
    }

}
